package com.example.project.config;

import com.example.project.config.shiro.CustomRealm;
import com.example.project.config.shiro.CustomWebSessionManager;
import com.example.project.config.shiro.ShiroLoginFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        filters.put("authc", new ShiroLoginFilter());
        filterChainDefinitionMap.put("/firm/**","anon");


        filterChainDefinitionMap.put("/tadmin/login", "anon");
        //swagger
        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");

        //静态资源
        filterChainDefinitionMap.put("/UploadFile/**", "anon");

        //ueditor
        filterChainDefinitionMap.put("/mh/ueditor/**", "anon");


        filterChainDefinitionMap.put("/**", "authc");
//        filterChainDefinitionMap.put("/**", "anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;

    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        defaultSecurityManager.setRealm(customRealm());
        defaultSecurityManager.setSessionManager(sessionManager());
        return defaultSecurityManager;
    }

    @Bean
    public CustomRealm customRealm() {
        CustomRealm customRealm = new CustomRealm();
        return customRealm;
    }

    @Bean
    public ShiroLoginFilter shiroLoginFilter(){
        return new ShiroLoginFilter();
    }

    //注解开启
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public DefaultWebSessionManager sessionManager() {
        CustomWebSessionManager customWebSessionManager = new CustomWebSessionManager();
        //session有效期1h
        customWebSessionManager.setGlobalSessionTimeout(3600000);
        customWebSessionManager.setSessionValidationInterval(3600000);
        customWebSessionManager.setSessionValidationSchedulerEnabled(true);

        return customWebSessionManager;
    }


//    @Bean
//    public MemoryConstrainedCacheManager shiroCacheManager() {
//        return new MemoryConstrainedCacheManager();
//    }
//
//    @Bean
//    @DependsOn({"shiroCacheManager"})
//    public CustomCredentialsMatcher hashedCredentialsMatcher(CacheManager cacheManager) {
//        CustomCredentialsMatcher hashedCredentialsMatcher = new CustomCredentialsMatcher(cacheManager);
//        return hashedCredentialsMatcher;
//    }
}